import {Component, OnInit} from '@angular/core';
import {AddWmsStockTransferComponent} from '../add-wms-stock-transfer.component';
import {ErrRes} from '../../../../../frame/global/err-res';
import {WmsStockTransfer, WmsStockTransferCondition} from '../../wms-stock-transfer.type';
import {Services} from '../../../../../frame/module/services';
import {UrlRou} from '../../../../../frame';
import {Zero3Alert} from '../../../../../frame/global/zero3-alert/zero3-alert.component';
import {Zero3Enum} from '../../../../../frame/enum/zero3-enum';
import * as fa from '@fortawesome/free-solid-svg-icons';
import {WmsWarehouseManage, WmsWarehouseManageCondition} from '../../../wms-warehouse-manage/wms-warehouse-manage.type';
import {WmsStockDetail} from '../../../wms-stock-detail/wms-stock-detail.type';
import {isNullOrUndefined} from "util";
import {
GooBaseGoodsSpecificationCondition} from '../../../../goods-management/goo-base-goods-specification/goo-base-goods-specification.type';
import {ActivatedRoute, Router} from '@angular/router';
import {BaseGoodsCondition} from '../../../../goods-management/goo-base-goods/base-goods.type';
import {HttpRequestClient} from '../../../../../frame/http-request-client/http-request-client';

@Component({
    selector: 'app-main-add-wms-stock-transfer',
    templateUrl: './main-add-wms-stock-transfer.component.html',
    styleUrls: ['./main-add-wms-stock-transfer.component.css']
})
export class MainAddWmsStockTransferComponent implements OnInit {

    errRes = new ErrRes();

    selectedGooBaseGoodsSpecification: any;
    selectedBaseGoods: any;
    wmsStockDetailMap = new Map<string, WmsStockDetail>();
    wmsStockDetailList = new Array<WmsStockDetail>();
    data;
    // 表格数据
    tableData = {
        title: [],
        data: []
    };
    domain = new WmsStockTransfer();
    extend = {
        domain: {
            serAudit: {
                nickname: ''
            },
            outWarehouseName: {
                items: [],
                nickname: '调出仓库名称',
                change: (event) => {
                    console.log(event);
                    this.domain.outWarehouseName = event.name;
                    this.domain.outWarehouseUuid = event.id;
                }
            },
            inWareHoseName: {
                items: [],
                nickname: '调入仓库名称',
                change: (event) => {
                    console.log(event);
                    this.domain.inWareHoseName = event.name;
                    this.domain.inWareHoseUuid = event.id;
                }
            },
            gooBaseGoodsSpecification: {
                items: [],
                nickname: '商品规格',
                change: (event) => {
                    console.log(event);
                    this.selectedGooBaseGoodsSpecification = event;
                }
            },
            gooBaseGoods: {
                items: [],
                nickname: '基础商品',
                change: (event) => {
                    this.selectedBaseGoods = event;
                    const condition = new GooBaseGoodsSpecificationCondition();
                    condition.fkGooBaseGoodsUuid = event.id;
                    Services.gooBaseGoodsSpecification.findByFkGooBaseGoodsUuid(condition).subscribe(item => {
                        if (!isNullOrUndefined(item) && item.code === 'success') {
                            console.log(item);
                            this.extend.domain.gooBaseGoodsSpecification.items = [];
                            item.domains.forEach(gooBaseGoodsSpecification => {
                                this.extend.domain.gooBaseGoodsSpecification.items.push(
                                    {id: gooBaseGoodsSpecification.uuid, name: gooBaseGoodsSpecification.serName}
                                );
                            });
                        }
                    });
                }
            },
            file: {
                oss: Services.aliOSS.simple(),
                change: (event) => {
                    console.log(event.target.value); // file name
                    this.extend.domain.file.oss.uploadFile((response, status, headers) => {
                        if (status === 200) {
                            this.extend.domain.file.oss.fileUrl = JSON.parse(response).detail.fileUrl;
                            this.domain.serFileUrl = this.extend.domain.file.oss.fileUrl;
                        } else {
                            this.extend.domain.file.oss.fileUrl = '';
                            this.domain.serFileUrl = this.extend.domain.file.oss.fileUrl;
                            Zero3Alert.req('fail');
                        }
                    });
                }
            },
        },
    };

    func = {
        default: () => {
            this.domain.serAudit = Zero3Enum.AUDIT_FLAG_WAIT_SUBMIT;
            this.domain.serUsing = Zero3Enum.ENABLE;
            this.domain.transferNumbers = 'DB32165465456456456';
        },
        loadDomain: (uuid: string) => {
            const condition: WmsStockTransferCondition = new WmsStockTransferCondition();
            condition.uuid = uuid;
            Services.wmsStockTransfer.info(condition).subscribe(item => {
                if (!isNullOrUndefined(item) && !isNullOrUndefined(item.domain)) {
                    this.domain = item.domain;
                    this.extend.domain.serAudit.nickname = Zero3Enum.AUDIT_FLAG.get(this.domain.serAudit);
                }
            });
        },
        save: {
            req: (event) => {
                console.log(this.domain);
                console.log(this.wmsStockDetailMap);
                this.wmsStockDetailList = Array.from(this.wmsStockDetailMap.values());
                const wmsStockTransfer = Object.assign({stockDetailList: this.wmsStockDetailList}, this.domain);
                Services.wmsStockTransfer.batchSave(wmsStockTransfer, this.func.save.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    HttpRequestClient.router.navigate([UrlRou.wmsStockTransfer.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        },
        addGooBaseGoodsSpecification: () => {
            const wmsStockDetail = new WmsStockDetail();
            wmsStockDetail.serItemUuid = '071cba55-e959-4fc0-848e-6fb10964a1ee';
            wmsStockDetail.serItemName = '华兰仕手表';
            wmsStockDetail.serSpecificationName = '规格名称';
            wmsStockDetail.serSpecificationUuid = '1ef0b265-4e3a-40ac-9743-abf69ad832de';
            wmsStockDetail.count = 0;
            wmsStockDetail.serItemImg = '商品图片';
            wmsStockDetail.serItemSerNum = '1';
            wmsStockDetail.serItemBrandUuid = '31fb2d68-c167-4ace-8b43-8c6cd6586cd5';
            wmsStockDetail.serItemBrandName = '品牌名称';
            wmsStockDetail.wmsStockType = 'STOCK_TRANSFER';
            console.log(wmsStockDetail);
            this.wmsStockDetailMap.set(Math.random().toString(), wmsStockDetail);
        },
        update: {
            req: (event) => {
                Services.wmsStockTransfer.update(this.domain, this.func.update.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    this.router.navigate([UrlRou.wmsStockTransfer.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        },
        deleteGooBaseGoodsSpecification: (event) => {
            this.wmsStockDetailMap.delete(event);
        }
    };

    body = {
        tr: {
            selectedUuid: new Map(),
            isSelected: (uuid) => {
                const value = this.body.tr.selectedUuid.get(uuid);
                return !isNullOrUndefined(value);
            },
            click: (event, uuid) => {
                const value = this.body.tr.selectedUuid.get(uuid);
                if (isNullOrUndefined(value)) {
                    this.body.tr.selectedUuid.set(uuid, '0');
                } else {
                    this.body.tr.selectedUuid.delete(uuid);
                }
            },
            dbclick: (event, uuid) => {
                console.log('dbclick');
                const condition = new WmsStockTransferCondition();
                condition.uuidList = [uuid];
                Services.wmsStockTransfer.modify(condition);
                console.log('fun modify');
            }
        }
    };

    head = {
        tr: {
            selectedAll: {
                icon: fa.faCheckCircle,
                click: (event) => {
                    if (this.body.tr.selectedUuid.size === 0) {
                        this.data.page.content.forEach(item => {
                            this.body.tr.selectedUuid.set(item.uuid, item.uuid);
                        });
                        this.head.tr.selectedAll.icon = fa.faCheck;
                    } else {
                        this.body.tr.selectedUuid.clear();
                        this.head.tr.selectedAll.icon = fa.faCheckCircle;
                    }
                }
            }
        },
    };
    constructor(public addWmsStockTransfer: AddWmsStockTransferComponent, private router: Router, private route: ActivatedRoute) {
    }
    // 添加商品
    addGoods(item) {
        console.log('-----', item);
        this.tableData.data.push(item);
    }
    ngOnInit() {
        this.func.default();
        this.addWmsStockTransfer.main = this;
        const uuid = this.route.snapshot.paramMap.get('uuid');
      /*  this.func.loadDomain(uuid);*/
        Services.wmsWarehouseManage.list(new WmsWarehouseManageCondition(), {
            page: 0,
            size: 999,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(item => {
            if (!isNullOrUndefined(item) && item.code === 'success') {
                console.log(item);
                this.extend.domain.outWarehouseName.items = [];
                this.extend.domain.inWareHoseName.items = [];
                item.page.content.forEach(wmsWarehouseManage => {
                    this.extend.domain.outWarehouseName.items.push(
                        {id: wmsWarehouseManage.uuid, name: wmsWarehouseManage.serName}
                    );
                    this.extend.domain.inWareHoseName.items.push(
                        {id: wmsWarehouseManage.uuid, name: wmsWarehouseManage.serName}
                    );
                });
            }
        });
        Services.baseGoods.list(new BaseGoodsCondition(), {
            page: 0,
            size: 999,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(item => {
            if (!isNullOrUndefined(item) && item.code === 'success') {
                this.extend.domain.gooBaseGoods.items = [];
                item.page.content.forEach(baseGoods => {
                    this.extend.domain.gooBaseGoods.items.push(
                        {id: baseGoods.uuid, name: baseGoods.serGoodsName}
                    );
                });
            }
        });
    }

}
